Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Text
Imports System.Windows.Forms
Imports System.Text.RegularExpressions

<ToolboxBitmap(GetType(TextBox))> _
Public Class ACTextMail
#Region "Variables"
   Private Shared m_valid As Boolean = False
   Public Shadows Event TextChanged As EventHandler
#End Region

#Region "Constructores"
   Public Sub New()
      InitializeComponent()
      m_valid = False
   End Sub
#End Region

#Region "Propiedades"
   Public Property ACTextBoxMail() As System.Windows.Forms.TextBox
      Get
         Return txtEmail
      End Get
      Set(ByVal value As System.Windows.Forms.TextBox)
         txtEmail = value
      End Set
   End Property
   Public Property ACEmailValido() As Boolean
      Get
         Return m_valid
      End Get
      Set(ByVal value As Boolean)
         m_valid = value
      End Set
   End Property
   Public Overrides Property Text() As String
      Get
         Return Me.txtEmail.Text
      End Get
      Set(ByVal value As String)
         Me.txtEmail.Text = value
         OnTextChanged(value, EventArgs.Empty)
      End Set
   End Property
#End Region

#Region "Eventos"

   Public Function ACValidarEmail(ByVal email As String) As Boolean
      Try
         Dim expresion As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
         If Regex.IsMatch(email, expresion) Then
            If Regex.Replace(email, expresion, [String].Empty).Length = 0 Then
               m_valid = True
               Return True
            Else
               m_valid = False
               Return False
            End If
         Else
            m_valid = False
            Return False
         End If
      Catch ex As Exception
         Throw ex
      End Try
   End Function

   Public Sub ACClear()
      Try
         RemoveHandler txtEmail.TextChanged, AddressOf txtEmail_TextChanged
         m_valid = True
         Me.txtEmail.Clear()
         Me.txtEmail.BackColor = Color.White
         AddHandler txtEmail.TextChanged, AddressOf txtEmail_TextChanged
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Private Sub txtEmail_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtEmail.TextChanged
      Try
         If Me.txtEmail.Text.Trim().Length = 0 Then
            m_valid = True
            Me.txtEmail.BackColor = Color.White
         Else
            If Not ACValidarEmail(Me.txtEmail.Text) Then
               m_valid = False
               Me.txtEmail.BackColor = Color.LightYellow
            Else
               m_valid = True
               Me.txtEmail.BackColor = Color.White
            End If
         End If
         Text = Me.txtEmail.Text
      Catch ex As Exception
         Throw ex
      End Try
   End Sub

   Public Overloads Sub OnTextChanged(ByVal sender As Object, ByVal e As EventArgs)
      RaiseEvent TextChanged(sender, e)
   End Sub
#End Region

End Class
